package com.assass2i.socialkiller;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

// Classe pour la connexion au serveur
public class HttpConnect {
	
	private static String 		URL_WS="http://192.168.43.170/WebSocialKiller/ServiceSocialKiller.asmx/";
	public static String idUser = null;
	public static String idFight = null;
	public static String staticNomCible = null;
	public static boolean attaqueEnCours = false;
	public static boolean pageCombat = false;
	public static boolean getProxiEnCours = false;
	public static boolean getFightStatEnCours = false;
	public static float targetLatitude = 0;
	public static float targetLongitude = 0;
	
	
	public static String connectHTTP(List<NameValuePair> param, String request)
	{
        try {
        	DefaultHttpClient httpClient = new DefaultHttpClient();
    		HttpPost httpPost = new HttpPost(URL_WS+request);
    		if (param!=null){
        		httpPost.setEntity(new UrlEncodedFormEntity(param));
    		}
    		HttpResponse response = httpClient.execute(httpPost);
    		HttpEntity entity = response.getEntity();
    		String strRep = "";
    		if (entity != null)
    			 strRep = EntityUtils.toString(entity);
    		
    		return strRep;
		} 
        catch (Exception e) {
			return "Erreur lors de l'appel au webservice";
		}  
	}
	
	public static ArrayList<String> parseResponse(String pResp)
	{
		String[] respFirstSlip = pResp.split(">");
		String[] respSecondSlip = respFirstSlip[2].split("<");
		ArrayList<String> parseResp = new ArrayList<String>();
		
		if(!respSecondSlip[1].equals("string"))
		{
			parseResp.add(respSecondSlip[0]);
			return parseResp;
		}
		else
		{
			String[] respThirdSlip = pResp.split("<string>");
			for(int i = 1; i<respThirdSlip.length; i++)
			{
				String[] s =respThirdSlip[i].split("</string>");
				parseResp.add(s[0]) ;
			}
			return parseResp;
		}
			
	}
}
